<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>子集</title>
</head>
<body>
  <script>
    function fn(nums) {
      // 输出数组
      let arr = [];
      function dfs(index, ans) {
        // 向输出数组添加当前组合
        arr.push(ans);
        // 从当前索引遍历继续递归，直到超出nums长度
        for (let i = index; i < nums.length; i++) {
          dfs(i + 1, [...ans, nums[i]]);
        }
      }
      dfs(0, []) // 执行递归
      return arr;
    }
    console.log(fn([1, 2, 3]))
  </script>
</body>
</html>